package com.yanqu.road.logic.thread;

import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.executor.GeneralThreadFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTaskManger {

	private List<ExecutorService> executor;

	private int threadPoolNum;

	public ThreadTaskManger(int threadPoolNum, String poolName, int threadNum) {
		this.threadPoolNum = threadPoolNum;
		executor = new ArrayList<>();
		for (int i = 0; i < threadPoolNum; i++) {
			executor.add(Executors.newFixedThreadPool(threadNum, new GeneralThreadFactory(poolName)));
		}
	}

	public ThreadTaskManger(int threadPoolNum, String poolName) {
		this.threadPoolNum = threadPoolNum;
		executor = new ArrayList<>();
		for (int i = 0; i < threadPoolNum; i++) {
			executor.add(Executors.newFixedThreadPool(1, new GeneralThreadFactory(poolName)));
		}
	}

	public void addTask(int taskId, GeneralTask generalTask) {
		int index = taskId % threadPoolNum;
		executor.get(index).execute(generalTask);
	}
}
